<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入发布用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帖子标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入帖子标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帖子内容" prop="content">
        <el-input
            v-model="queryParams.content"
            placeholder="请输入帖子标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帖子状态" prop="status">
        <el-select style="width: 200px" v-model="queryParams.status" placeholder="请选择帖子状态" clearable>
          <el-option
              v-for="dict in forum_post_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['forum:post:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['forum:post:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['forum:post:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['forum:post:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="80" label="帖子id" align="center" prop="postId" />
      <el-table-column label="发布id·用户" align="center" prop="userId">
        <template #default="scope">
          {{ scope.row.userId }}·
          {{ scope.row.nickName }}
        </template>
      </el-table-column>
      <el-table-column label="帖子标题" align="center" prop="title" show-overflow-tooltip/>
      <el-table-column label="文字内容" align="center" prop="content" show-overflow-tooltip/>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column width="80" label="帖子状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="forum_post_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column width="80" label="图片" align="center" prop="images" >
        <template #default="scope">
          <el-tag v-if="scope.row.hasImage == 0">无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['forum:post:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['forum:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改论坛帖子对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发布用户" prop="userId">
          <el-input disabled v-model="form.userId" placeholder="请输入发布用户ID" />
        </el-form-item>
        <el-form-item label="帖子标题" prop="title">
          <el-input disabled v-model="form.title" placeholder="请输入帖子标题" />
        </el-form-item>
        <el-form-item label="文字内容">
          <el-input disabled v-model="form.content" type="textarea"/>
        </el-form-item>
        <!--<el-form-item label="是否允许评论" prop="allowComment">
          <el-input v-model="form.allowComment" placeholder="请输入是否允许评论" />
        </el-form-item>
        <el-form-item label="是否公开" prop="isPublic">
          <el-input v-model="form.isPublic" placeholder="请输入是否公开" />
        </el-form-item>
        <el-form-item label="是否置顶" prop="isTop">
          <el-input v-model="form.isTop" placeholder="请输入是否置顶" />
        </el-form-item>
        <el-form-item label="是否推荐" prop="isRecommend">
          <el-input v-model="form.isRecommend" placeholder="请输入是否推荐" />
        </el-form-item>
        <el-form-item label="发布或更新帖子时的IP地址" prop="ipaddr">
          <el-input v-model="form.ipaddr" placeholder="请输入发布或更新帖子时的IP地址" />
        </el-form-item>
        <el-form-item label="实体地址" prop="location">
          <el-input v-model="form.location" placeholder="请输入实体地址" />
        </el-form-item>-->
        <el-form-item label="帖子状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in forum_post_status"
                :key="dict.value"
                :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import { listPost, getPost, delPost, addPost, updatePost } from "@/api/forum/post";

const { proxy } = getCurrentInstance();
const { forum_post_status } = proxy.useDict('forum_post_status');

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    title: null,
    content: null,
    likeCount: null,
    commentCount: null,
    viewCount: null,
    collectCount: null,
    status: null,
    allowComment: null,
    isPublic: null,
    isTop: null,
    isRecommend: null,
    ipaddr: null,
    location: null,
    hasImage: null,
    images: null,
  },
  rules: {
    userId: [
      { required: true, message: "发布用户ID不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "文字内容不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询论坛帖子列表 */
function getList() {
  loading.value = true;
  listPost(queryParams.value).then(response => {
    postList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    postId: null,
    userId: null,
    title: null,
    content: null,
    likeCount: null,
    commentCount: null,
    viewCount: null,
    collectCount: null,
    status: null,
    delFlag: null,
    allowComment: null,
    isPublic: null,
    isTop: null,
    isRecommend: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    ipaddr: null,
    location: null,
    hasImage: null,
    images: null,
    remark: null
  };
  proxy.resetForm("postRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加论坛帖子";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _postId = row.postId || ids.value
  getPost(_postId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改论坛帖子";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate(valid => {
    if (valid) {
      if (form.value.postId != null) {
        updatePost(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPost(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _postIds = row.postId || ids.value;
  proxy.$modal.confirm('是否确认删除论坛帖子编号为"' + _postIds + '"的数据项？').then(function() {
    return delPost(_postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('forum/post/export', {
    ...queryParams.value
  }, `post_${new Date().getTime()}.xlsx`)
}

getList();
</script>
